package cn.youwan.market.database;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import cn.youwan.market.download.AppDownloadService;
import cn.youwan.market.entity.DownFile;
import cn.youwan.market.entity.PromotionListbean.Appbean;
import cn.youwan.market.utils.Const;
import cn.youwan.market.utils.Utils;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class MyDao {
	SQLiteDatabase dataBase;
	Context context;
	DBHelper helper;

	public MyDao(Context context) {
		this.context = context;
		helper = new DBHelper(context);
		dataBase = helper.getWritableDatabase();
	}

	public void closeDatBase() {
		if (dataBase != null && dataBase.isOpen()) {
			dataBase.close();
			helper.close();
		}
	}

	/*
	 * 查询联系人基本数据
	 */
	public List<DownFile> queryAllDownApk() {
		List<DownFile> list = new ArrayList<DownFile>();
		String[] projection = { "_id", "company", "name", "rate", "icon",
				"pkg", "url", "file_size", "state", "appid" };
		Cursor cursor = dataBase.query("apks", projection, null, null, null,
				null, null);
		while (cursor.moveToNext()) {
			DownFile bean = new DownFile();
			bean.company = cursor.getString(1);
			bean.name = cursor.getString(2);
			bean.rate = cursor.getString(3);
			bean.icon = cursor.getString(4);
			bean.pkg = cursor.getString(5);
			String url = cursor.getString(6);
			bean.url = url;
			bean.fileSize = Integer.parseInt(cursor.getString(7));
			bean.state = cursor.getInt(8);
			bean.appId = cursor.getString(9);
			list.add(bean);
		}
		cursor.close();
		return list;
	}

	/*
	 * 查询可安装的apk列表
	 */
	public List<DownFile> queryInStallDownApk() {
		List<DownFile> list = new ArrayList<DownFile>();
		String[] projection = { "_id", "company", "name", "rate", "icon",
				"pkg", "url", "file_size", "state", "appid" };
		Cursor cursor = dataBase.query("apks", projection, "state=?",
				new String[] { "1" }, null, null, null);
		while (cursor.moveToNext()) {
			DownFile bean = new DownFile();
			bean.company = cursor.getString(1);
			bean.name = cursor.getString(2);
			bean.rate = cursor.getString(3);
			bean.icon = cursor.getString(4);
			bean.pkg = cursor.getString(5);
			String url = cursor.getString(6);
			bean.url = url;
			bean.fileSize = Integer.parseInt(cursor.getString(7));
			bean.state = cursor.getInt(8);
			bean.appId = cursor.getString(9);
			list.add(bean);
		}
		cursor.close();
		return list;
	}

	/*
	 * 查询没有下载完成的apk列表 用于正在下载的列表
	 */
	public List<Appbean> queryStallDownApk() {
		List<Appbean> list = new ArrayList<Appbean>();
		String[] projection = { "_id", "company", "name", "rate", "icon",
				"pkg", "url", "file_size", "state", "appid" };
		Cursor cursor = dataBase.query("apks", projection, "state=?",
				new String[] { "0" }, null, null, null);
		while (cursor.moveToNext()) {
			Appbean bean = new Appbean();
			bean.setAppCompany(cursor.getString(1));
			bean.setAppName(cursor.getString(2));
			bean.setRate(Float.parseFloat(cursor.getString(3)));
			bean.setAppIcon(cursor.getString(4));
			bean.setPkg(cursor.getString(5));
			bean.setId(cursor.getString(9));
			File file = new File(Const.APKS + Utils.md5(bean.getPkg()) + ".apk");
			if (file.exists()) {
				bean.setProgress((int) file.length());
			}
			String url = cursor.getString(6);
			bean.setUrl(url);
			bean.setAppSize(Integer.parseInt(cursor.getString(7)));
			if (AppDownloadService.getDownloadingApps() != null
					&& AppDownloadService.getDownloadingApps().containsKey(
							bean.getPkg())) {
				bean.setDownState(AppDownloadService.getDownloadingApps()
						.get(bean.getPkg()).getDownState());
			} else {
				bean.setDownState(5);
			}
			list.add(bean);
		}
		cursor.close();
		return list;
	}

	/*
	 * 查询没有下载完成的apk列表数量
	 */
	public int queryStallDownApkNum() {
		String[] projection = { "_id", "company", "name", "rate", "icon",
				"pkg", "url", "file_size", "state", "appid" };
		Cursor cursor = dataBase.query("apks", projection, "state=?",
				new String[] { "0" }, null, null, null);
		int count = 0;
		if (cursor != null) {
			count = cursor.getCount();
		}
		cursor.close();
		return count;
	}

	public DownFile queryDownApk(String pkg) {
		String[] projection = { "_id", "pkg", "url", "file_size", "state",
				"appid" };
		Cursor cursor = dataBase.query("apks", projection, "pkg=?",
				new String[] { pkg }, null, null, null);
		if (cursor == null || cursor.getCount() == 0) {
			return null;
		}
		DownFile bean = new DownFile();
		while (cursor.moveToNext()) {
			bean.pkg = cursor.getString(1);
			String url = cursor.getString(2);
			bean.url = url;
			bean.fileSize = Integer.parseInt(cursor.getString(3));
			bean.state = cursor.getInt(4);
			bean.appId = cursor.getString(5);
		}
		cursor.close();
		return bean;
	}

	public long insert(DownFile cb) {
		Cursor cursor = dataBase.query("apks", new String[] { "_id" }, "pkg=?",
				new String[] { cb.pkg }, null, null, null);
		if (cursor != null && cursor.getCount() != 0) {
			return 0;
		}
		ContentValues v = new ContentValues();
		v.put("company", cb.company);
		v.put("name", cb.name);
		v.put("rate", cb.rate);
		v.put("icon", cb.icon);
		v.put("pkg", cb.pkg);
		v.put("url", cb.url);
		v.put("file_size", String.valueOf(cb.fileSize));
		v.put("state", cb.state);
		v.put("appid", cb.appId);
		return dataBase.insert("apks", null, v);
	}

	public void updateState(String pkg, int state) {
		ContentValues v = new ContentValues();
		v.put("state", state);
		dataBase.update("apks", v, "pkg=?", new String[] { pkg });
	}

	public void delete(String pkg) {
		dataBase.delete("apks", "pkg=?", new String[] { pkg });
	}

}
